package org.dromara.module.api;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.constant.ResponseCode;
import org.dromara.common.core.utils.GsonResultBean;
import org.dromara.common.core.utils.Tools;
import org.dromara.module.domain.Product;
import org.dromara.module.domain.ProductType;
import org.dromara.module.domain.WxUser;
import org.dromara.module.service.IProductService;
import org.dromara.module.service.IProductTypeService;
import org.dromara.module.service.IWxUserService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *@Author: ZhouWei
 *@Description:  商品类型相关接口
 *@Date 2018-05-10
 */
@Slf4j
@SaIgnore
@Component
@RestController
@RequestMapping("/api/product/type")//url:模块/资源/{}/细分
public class ProductTypeApiController {

    @Resource
    private IProductTypeService productTypeService;
    @Resource
    private IProductService productService;
    @Resource
    private IWxUserService userService;

    @Value("${ruoyi.imgUrl}")
    private String imgUrl;//微信支付的回调地址

    /**
     *  获取所有分类
     * @param
     * @throws Exception
     */
    @PostMapping(value = "/list")
    public JSONObject indexPage(@RequestBody String requestParam) throws Exception{
        JSONObject result = null;
        try{
            JSONObject jsonRequest = JSONUtil.parseObj(requestParam);
            int user_id = jsonRequest.containsKey("user_id") ? jsonRequest.getInt("user_id") : 0;
            int type_id = jsonRequest.containsKey("type_id") ? jsonRequest.getInt("type_id") : 0;
            int pageIndex = jsonRequest.containsKey("pageIndex") ? jsonRequest.getInt("pageIndex") : 1;
            int pageSize = jsonRequest.containsKey("pageSize") ? jsonRequest.getInt("pageSize") : 10;

            WxUser user = userService.getById(user_id);

            Map<String,Object> map = new HashMap<String, Object>();
            //1.先获取父节点
            QueryWrapper<ProductType> qw = new QueryWrapper<>();
            qw.eq("state",0);
            qw.eq("parent_id",type_id);
            qw.eq("is_show", 1);
            qw.orderByAsc("sort");
            List<ProductType> list = productTypeService.list(qw);
            if(CollUtil.isNotEmpty(list)){
                for (ProductType type : list){
                    if(StrUtil.isNotEmpty(type.getImgUrl())){
                        type.setImgUrl(imgUrl + type.getImgUrl());
                    }

                    QueryWrapper<ProductType> qwChild = new QueryWrapper<>();
                    qwChild.eq("state",0);
                    qwChild.eq("parent_id",type.getId());
                    qwChild.orderByAsc("sort");
                    List<ProductType> listChild = productTypeService.list(qwChild);
                    if(CollUtil.isNotEmpty(listChild)){
                        for (ProductType typeChild : list){
                            typeChild.setImgUrl(imgUrl + typeChild.getImgUrl());
                        }
                    }
                    type.setChildren(listChild);
                    type.setChildCount(listChild.size());
                }
            }

            if(list.size() > 0){
                //WxUser user, String productName, int typeId, int typeChildId, int pageIndex, int pageSize
                List<Product> products = productService.queryProductList(user,"", list.get(0).getId(),0,pageIndex,pageSize);
                map.put("products",products);
            }
            map.put("types",list);
            result = Tools.getJson(new GsonResultBean<>(ResponseCode.OK,"成功",list.size(), map),false,false);
        }catch (Exception e){
            result = Tools.getJson(new GsonResultBean<>(ResponseCode.FAIL,"系统异常",e.getMessage()),true,true);
            log.error(e.toString(), e);
        }
        return result;
    }

    /** 获取所有分类
     * @param
     * @throws Exception
     */
    @PostMapping(value="/childList")
    public JSONObject childlist(@RequestBody String requestParam) throws Exception{
        JSONObject result = null;
        try{
            JSONObject jsonRequest = JSONUtil.parseObj(requestParam);
            int user_id = jsonRequest.containsKey("user_id") ? jsonRequest.getInt("user_id") : 0;
            int type_id = jsonRequest.containsKey("type_id") ? jsonRequest.getInt("type_id") : 0;
            int type_child_id = jsonRequest.containsKey("type_child_id") ? jsonRequest.getInt("type_child_id") : 0;
            int pageIndex = jsonRequest.containsKey("pageIndex") ? jsonRequest.getInt("pageIndex") : 1;
            int pageSize = jsonRequest.containsKey("pageSize") ? jsonRequest.getInt("pageSize") : 10;

            WxUser user = userService.getById(user_id);

            Map<String,Object> map = new HashMap<String, Object>();
            QueryWrapper<ProductType> qw = new QueryWrapper<>();
            qw.eq("parent_id",type_id);
            qw.orderByAsc("sort");
            //1.先获取父节点
            List<ProductType> types = productTypeService.list(qw);
            if(CollUtil.isNotEmpty(types)){
                for (ProductType typeChild : types){
                    if(StrUtil.isNotEmpty(typeChild.getImgUrl())){
                        typeChild.setImgUrl(imgUrl + typeChild.getImgUrl());
                    }
                }
            }
            if(types.size() > 0){
                List<Product> products = productService.queryProductList(user,"", type_id, type_child_id, pageIndex, pageSize);
                map.put("products",products);
            }
            map.put("types",types);
            result = Tools.getJson(new GsonResultBean<>(ResponseCode.OK,"成功",types.size(), map),false,false);
        }catch (Exception e){
            result = Tools.getJson(new GsonResultBean<>(ResponseCode.FAIL,"系统异常",e.getMessage()),true,true);
            log.error(e.toString(), e);
        }
        return result;
    }
}
